﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "overallsituation.h"
#include "thread.h"
#include <QMainWindow>
#include <QtCore>
#include <libudev.h>
#include <QDebug>
#include <QLabel>
#include <QWidget>
#include<QLineEdit>
#include<QPushButton>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include <QListWidget>
#include <QTableWidget>
#include <QDesktopServices>

#include <openssl/des.h>
#include <sstream>


#include <vector>//c++STL容器
#include <QFileDialog>//文件查找
#include<QMediaPlayer>//播发器
#include<QMediaPlaylist>//播放队列
#include<QVideoWidget>//视频显示窗口

#include <QStylePainter>
#include <QScrollArea>
#include <QSystemTrayIcon>
#include <qdesktopwidget.h>
#include <QGraphicsView>
#include <QGraphicsAnchor>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class LoginDialog : public QDialog {
public:
    LoginDialog(QWidget* parent = nullptr) : QDialog(parent) {

        if(config.MUI=="English.qm")

            setWindowTitle(tr("Login"));

        else
            setWindowTitle(tr("登录"));
        setFixedSize(400, 200);

        if(config.MUI=="English.qm")

            label = new QLabel(tr("User ID:"), this);
        else
            label = new QLabel(tr("用户编号："), this);
        label->setStyleSheet("border: none; font: 10pt \"方正粗黑宋简体\"; color: black;");
        label->setFixedSize(100, 30);
        usernameLineEdit = new QLineEdit(this);

        usernameLineEdit->setFixedSize(150, 30);
        if(config.MUI=="English.qm")

            label2 = new QLabel(tr("Password:"), this);
        else
            label2 = new QLabel(tr("密码："), this);
        label2->setStyleSheet("border: none; font: 10pt \"方正粗黑宋简体\"; color: black;");
        label2->setFixedSize(100, 30);

        passwordLineEdit = new QLineEdit(this);
        passwordLineEdit->setEchoMode(QLineEdit::Password);
        passwordLineEdit->setFixedSize(150, 30);

        if(config.MUI=="English.qm")
        {
            loginButton = new QPushButton(tr("Login"), this);
            cancelButton = new QPushButton(tr("exit"), this);
        }
        else
        {
            loginButton = new QPushButton(tr("登录"), this);
            cancelButton = new QPushButton(tr("退出"), this);
        }

        // Set the size of the loginButton
        loginButton->setFixedSize(120, 30); // Set the width to 100 pixels and height to 30 pixels (adjust as needed).

        // Set the size of the cancelButton
        cancelButton->setFixedSize(120, 30); // Set the width to 100 pixels and height to 30 pixels (adjust as needed).

        //给登录和退出按钮变颜色，并且在左边插入图标
        // Set the stylesheet for loginButton to change its color and add an icon on the left side.
        loginButton->setStyleSheet("background-color: #525253; color: white; /*border: 2px solid #4CAF50;*/ padding-left: 20px; padding-right: 20px;");
        loginButton->setIcon(QIcon(":/new/prefix1/ICON/确定.png")); // Replace ":/path/to/login-icon.png" with the actual path to your login icon.

        // Set the stylesheet for cancelButton to change its color and add an icon on the left side.
        cancelButton->setStyleSheet("background-color: #525253; color: white; /*border: 2px solid #f44336;*/ padding-left: 20px; padding-right: 20px;");
        cancelButton->setIcon(QIcon(":/new/prefix1/ICON/取消.png")); // Replace ":/path/to/cancel-icon.png" with the actual path to your cancel icon.


        buttonLayout = new QHBoxLayout;
        buttonLayout->addWidget(loginButton);
        buttonLayout->addWidget(cancelButton);

        buttonLayout1 = new QHBoxLayout;
        buttonLayout1->addWidget(label);
        buttonLayout1->addWidget(usernameLineEdit);

        buttonLayout2 = new QHBoxLayout;
        buttonLayout2->addWidget(label2);
        buttonLayout2->addWidget(passwordLineEdit);

        buttonLayout3= new QVBoxLayout;

        buttonLayout3->addLayout(buttonLayout1);
        buttonLayout3->addLayout(buttonLayout2);

        buttonLayout4= new QHBoxLayout;
        buttonLayout4->addLayout(buttonLayout3);
        buttonLayout4->addSpacing(30); // Adjust the spacing as needed
        imageLabel = new QLabel(this);

        // Load the image
        QPixmap pixmap(":/new/prefix1/DeviceMng/finger.jpg");

        // Set the pixmap as the label's pixmap
        imageLabel->setPixmap(pixmap);
        buttonLayout4->addWidget(imageLabel);
        mainLayout = new QVBoxLayout(this);

        connect(loginButton, &QPushButton::clicked, this, &LoginDialog::accept);
        connect(cancelButton, &QPushButton::clicked, this, &LoginDialog::reject);


    }
    ~LoginDialog()
    {

        QString processName = "pkill -f \"onboard\"";
               QProcess process;
               process.startDetached(processName);
        qDebug()<<"~LoginSecondDialog";
        delete label;
        delete label2;
        delete imageLabel;
        delete loginButton;
        delete cancelButton;
        delete buttonLayout;
        delete buttonLayout1;
        delete buttonLayout2;
        delete buttonLayout3;
        delete buttonLayout4;

        delete mainLayout;
        delete usernameLineEdit;
        delete passwordLineEdit;
    };

    void Open()
    {
        //不会阻塞程序
                    QString processName = QString("onboard");
                    QProcess process;
                    process.startDetached(processName);

        mainLayout->addLayout(buttonLayout4);
        //label和usernameLineEdit//并排排列
        mainLayout->addLayout(buttonLayout);

    }
    void Close()
    {
        QString processName = "pkill -f \"onboard\"";
               QProcess process;
               process.startDetached(processName);
        usernameLineEdit->setText("");
        passwordLineEdit->setText("");
        mainLayout->removeItem(buttonLayout);
        mainLayout->removeItem(buttonLayout4);
    }

    QLabel* label ;
    QLabel* label2 ;

    QLabel *imageLabel;
    QPushButton* loginButton;
    QPushButton* cancelButton;
    QHBoxLayout* buttonLayout ;
    QHBoxLayout* buttonLayout1 ;
    QHBoxLayout* buttonLayout2 ;
    QVBoxLayout* buttonLayout3 ;
    QHBoxLayout* buttonLayout4 ;
    QVBoxLayout* mainLayout;
    QLineEdit* usernameLineEdit;
    QLineEdit* passwordLineEdit;

};
class LoginSecondDialog : public QDialog {
public:

    LoginSecondDialog(QWidget* parent = nullptr) : QDialog(parent) {


        if(config.MUI=="English.qm")

            setWindowTitle(tr("Login"));

        else
            setWindowTitle(tr("登录"));
        setFixedSize(400, 200);
        if(config.MUI=="English.qm")

            label = new QLabel(tr("User ID:"), this);
        else
            label = new QLabel(tr("用户编号："), this);
        label->setStyleSheet("border: none; font: 10pt \"方正粗黑宋简体\"; color: black;");
        label->setFixedSize(100, 30);
        usernameLineEdit = new QLineEdit(this);
        usernameLineEdit->setFixedSize(150, 30);
        if(config.MUI=="English.qm")

            label2 = new QLabel(tr("Password:"), this);
        else
            label2 = new QLabel(tr("密码："), this);
        label2->setStyleSheet("border: none; font: 10pt \"方正粗黑宋简体\"; color: black;");
        label2->setFixedSize(100, 30);
        passwordLineEdit = new QLineEdit(this);
        passwordLineEdit->setEchoMode(QLineEdit::Password);
        passwordLineEdit->setFixedSize(150, 30);

        if(config.MUI=="English.qm")

            label3 = new QLabel(tr("serial number:"), this);
        else
            label3 = new QLabel(tr("序列号:"), this);
        label3->setStyleSheet("border: none; font: 10pt \"方正粗黑宋简体\"; color: black;");
        label3->setFixedSize(100, 30);

        serialnumLineEdit = new QLineEdit(this);
        serialnumLineEdit->setFixedSize(150, 30);


        if(config.MUI=="English.qm")
        {
            loginButton = new QPushButton(tr("Login"), this);
            cancelButton = new QPushButton(tr("exit"), this);
        }
        else
        {
            loginButton = new QPushButton(tr("登录"), this);
            cancelButton = new QPushButton(tr("退出"), this);
        }
        // Set the size of the loginButton
        loginButton->setFixedSize(120, 30); // Set the width to 100 pixels and height to 30 pixels (adjust as needed).

        // Set the size of the cancelButton
        cancelButton->setFixedSize(120, 30); // Set the width to 100 pixels and height to 30 pixels (adjust as needed).

        //给登录和退出按钮变颜色，并且在左边插入图标
        // Set the stylesheet for loginButton to change its color and add an icon on the left side.
        loginButton->setStyleSheet("background-color: #525253; color: white; /*border: 2px solid #4CAF50;*/ padding-left: 20px; padding-right: 20px;");
        loginButton->setIcon(QIcon(":/new/prefix1/ICON/确定.png")); // Replace ":/path/to/login-icon.png" with the actual path to your login icon.

        // Set the stylesheet for cancelButton to change its color and add an icon on the left side.
        cancelButton->setStyleSheet("background-color: #525253; color: white; /*border: 2px solid #f44336;*/ padding-left: 20px; padding-right: 20px;");
        cancelButton->setIcon(QIcon(":/new/prefix1/ICON/取消.png")); // Replace ":/path/to/cancel-icon.png" with the actual path to your cancel icon.


        buttonLayout = new QHBoxLayout;
        buttonLayout->addWidget(loginButton);
        buttonLayout->addWidget(cancelButton);

        buttonLayout1 = new QHBoxLayout;
        buttonLayout1->addWidget(label);
        buttonLayout1->addWidget(usernameLineEdit);

        buttonLayout2 = new QHBoxLayout;
        buttonLayout2->addWidget(label2);
        buttonLayout2->addWidget(passwordLineEdit);

        buttonLayout3 = new QHBoxLayout;
        buttonLayout3->addWidget(label3);
        buttonLayout3->addWidget(serialnumLineEdit);

        buttonLayout4= new QVBoxLayout;

        buttonLayout4->addLayout(buttonLayout1);
        buttonLayout4->addLayout(buttonLayout2);
        buttonLayout4->addLayout(buttonLayout3);

        buttonLayout5= new QHBoxLayout;
        buttonLayout5->addLayout(buttonLayout4);
        buttonLayout5->addSpacing(30); // Adjust the spacing as needed
        imageLabel = new QLabel(this);

        // Load the image
        QPixmap pixmap(":/new/prefix1/DeviceMng/finger.jpg");

        // Set the pixmap as the label's pixmap
        imageLabel->setPixmap(pixmap);

        buttonLayout5->addWidget(imageLabel);
        mainLayout = new QVBoxLayout(this);

        connect(loginButton, &QPushButton::clicked, this, &LoginDialog::accept);
        connect(cancelButton, &QPushButton::clicked, this, &LoginDialog::reject);

    }
    void Open()
    {


        //不会阻塞程序
                    QString processName = QString("onboard");
                    QProcess process;
                    process.startDetached(processName);

        mainLayout->addLayout(buttonLayout5);
        //label和usernameLineEdit//并排排列
        mainLayout->addLayout(buttonLayout);



    }
    void Close()
    {
        QString processName = "pkill -f \"onboard\"";
               QProcess process;
               process.startDetached(processName);
        usernameLineEdit->setText("");
        passwordLineEdit->setText("");
        serialnumLineEdit->setText("");
        mainLayout->removeItem(buttonLayout5);
        mainLayout->removeItem(buttonLayout);
    }

    ~LoginSecondDialog()
    {
        QString processName = "pkill -f \"onboard\"";
               QProcess process;
               process.startDetached(processName);
        delete  label ;
        delete label2 ;
        delete label3 ;
        delete imageLabel;
        delete loginButton;
        delete cancelButton;
        delete buttonLayout ;
        delete buttonLayout1 ;
        delete buttonLayout2 ;
        delete buttonLayout3 ;
        delete buttonLayout4 ;
        delete buttonLayout5 ;

        delete mainLayout;
        delete usernameLineEdit;
        delete passwordLineEdit;
        delete  serialnumLineEdit;
    }
    QLabel* label ;
    QLabel* label2 ;
    QLabel* label3 ;
    QLabel *imageLabel;
    QPushButton* loginButton;
    QPushButton* cancelButton;
    QHBoxLayout* buttonLayout ;
    QHBoxLayout* buttonLayout1 ;
    QHBoxLayout* buttonLayout2 ;
    QHBoxLayout* buttonLayout3 ;
    QVBoxLayout* buttonLayout4 ;
    QHBoxLayout* buttonLayout5 ;

    QVBoxLayout* mainLayout;
    QLineEdit* usernameLineEdit;
    QLineEdit* passwordLineEdit;
    QLineEdit*  serialnumLineEdit;
};
class ImageViewer : public QGraphicsView {
    Q_OBJECT

public:
    ImageViewer(QWidget *parent = nullptr) : QGraphicsView(parent) {
        scene = new QGraphicsScene(this);
        setScene(scene);

        // 初始时不加载图片，留空
        pixmapItem = nullptr;

        // 启用鼠标拖动
        setDragMode(QGraphicsView::ScrollHandDrag);

        // 启用滚轮缩放（可选）
        setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
        setResizeAnchor(QGraphicsView::AnchorUnderMouse);
        setRenderHint(QPainter::Antialiasing);
    }

    // 切换图片的方法
    void setImage(const QString &imagePath) {
        // 如果有当前的pixmapItem，则先从场景中移除它
        if (pixmapItem) {
            scene->removeItem(pixmapItem);
            delete pixmapItem; // 注意：这里需要手动删除，因为我们是用new创建的
            pixmapItem = nullptr;
        }

        // 加载新图片
        QPixmap pixmap(imagePath);
        if (!pixmap.isNull()) {
            // 创建一个新的QGraphicsPixmapItem并将其添加到场景中
            pixmapItem = scene->addPixmap(pixmap);

            // 可选：设置场景的大小以匹配图片大小
            scene->setSceneRect(pixmap.rect());


            // 确保视图更新以显示新图片
            fitInView(pixmapItem, Qt::KeepAspectRatio);
        }
    }

protected:
    // 可选：重写wheelEvent以自定义滚轮缩放行为
    void wheelEvent(QWheelEvent *event) override {
        const qreal delta = event->angleDelta().y() / 120.0;
        const qreal scaleFactor = 1.15;
        if (delta > 0) {
            // 放大
            scale(scaleFactor, scaleFactor);
        } else {
            // 缩小
            scale(1.0 / scaleFactor, 1.0 / scaleFactor);
        }
    }

private:
    QGraphicsScene *scene;
    QGraphicsPixmapItem *pixmapItem;
};
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    ImageViewer *viewer;
    LoginDialog dialog;
    LoginSecondDialog SecondDialog;
    //    QHBoxLayout* layout[SYSTEMSUM];
    //    QLabel* imageLabel1[SYSTEMSUM];
    //    QLabel* imageLabel2[SYSTEMSUM];
    // 创建临界消息框
    QMessageBox *messageBox ;
    ThreadPool *MonitorThread;
    ThreadPool *RetransmissionThread;
//    QList<QCameraInfo>  Infolist;       //用于保存可用摄像头
//    QCamera *Camera1;                    //使用这个类，实例化出来，用指针接收
//    QCameraImageCapture *ImageCapture;


    bool IsCamera;
    //HTTP http;
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void Startkeyboard();
    void handleDeviceEvent(int socket);

    inline void Send(int ParentID,int SubID,QJsonObject Data);
    void onConnected();
    void onDisconnected();
    void onReadyRead();
    void onError(QAbstractSocket::SocketError socketError);




    // void RegisterDevice();//注册GUID
    void ResetTing();//重置
    void Refresh();//刷新
    //void CheckDriveChanges();////获取新的盘符
    void getNextVideoAndPlay();////播放结束循环播放
    void getNextAudioAndPlay();////播放结束循环播放

    void ThreadJoin(int serialportnum,QString vid ,QString pid,QString serialnumber);//兼容型号配对
    void onMediaStatusChanged(QMediaPlayer::MediaStatus status);////结束获取
    void  onMediaStatusChanged_Audio(QMediaPlayer::MediaStatus status);////结束获取
    void displayDiary(const QString& filePath);//打开日记
    void displayPhoto(const QString& filePath);//打开照片

    bool SetLabel(int Index,QString Text,QString TextInfo,QString Textprompt,int Display);//修改标签控件
    bool OpenLoginDialog(QString& UserID,QString& Password,QString Serialnum,int sign,int index);
    //登录
    //bool nativeEvent(const QByteArray &eventType, void *message, long *result);//监听

    void  Reconnect();

    void a();
    void b(QString Path);

    void c();
    bool Config_Heavylod();
    void play(QString fileName,unsigned int winId);

    void on_photograph_clicked();

   void progressBar(int Index,int Valer,int variable);
private slots:
    void dataReceive();
    void on_comboBox_textActivated(const QString &arg1);
    void on_label_CALLIcon_Clicked();
    void on_label_WOKIcon_Clicked();
    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_row_activated(int index);

    void on_list_activated(int index);

    void on_configure_button_clicked();

    void on_comboBox_num_activated(int index);

    void on_pushButton_6_clicked();

    void on_label_101_Clicked();
    void on_label_102_Clicked();
    void on_label_103_Clicked();
    void on_label_104_Clicked();
    void on_label_105_Clicked();
    void on_label_106_Clicked();
    void on_label_107_Clicked();
    void on_label_108_Clicked();
    void on_label_109_Clicked();
    void on_label_110_Clicked();
    void on_label_111_Clicked();
    void on_label_112_Clicked();
    void on_label_113_Clicked();
    void on_label_114_Clicked();
    void on_label_115_Clicked();
    void on_label_116_Clicked();
    void on_label_117_Clicked();
    void on_label_118_Clicked();
    void on_label_119_Clicked();
    void on_label_120_Clicked();
    void on_label_121_Clicked();
    void on_label_122_Clicked();
    void on_label_123_Clicked();
    void on_label_124_Clicked();
    void on_label_125_Clicked();
    void on_label_icon_Clicked();
    void on_label_58_Clicked();
    void on_save_configButton_clicked();
    //void handleThreadNotice(int indentID,QString title);
    //   void handleThreadUpload(int indentID,int SerialportNum);
    void handlePrint(QString text);
    void on_pushButton_clicked();

    int  calculateNumbers(int X, int Y);

    void on_pushButton_7_clicked();

    void on_pushButton_8_clicked();

    //    void on_horizontalSlider_2_valueChanged(int value);

    //void on_horizontalSlider_valueChanged(int value);

    void on_pushButton_9_clicked();

    void on_Query_clicked();

    void on_tableWidget_cellClicked(int row, int column);

    void on_listWidget_itemClicked(QListWidgetItem *item);

    void on_pushButton_10_clicked();

    void on_pushButton_message_clicked();

    void on_tabWidget_currentChanged(int index);

    void on_pushButton_Up_clicked();

    void on_pushButton_down_clicked();

    void on_pushButton_right_clicked();

    void on_pushButton_left_clicked();

    void on_radioButton_appoint_clicked(bool checked);

    void on_radioButton_without_clicked(bool checked);

    //void on_radioButton_resetradio_clicked(bool checked);

    void on_pushButton_audio1_clicked();

    void on_pushButton_audio2_clicked();

    void on_pushButton_audio3_clicked();

    void on_pushButton_audio4_clicked();


    //    void on_horizontalSlider_audio1_valueChanged(int value);

    //    void on_horizontalSlider_audio2_valueChanged(int value);

    void on_pushButton_11_clicked();

    void on_pushButton_keep_clicked();

    void on_pushButton_12_clicked();


    void on_pushButton_17_clicked();

    void on_pushButton_16_clicked();

    void on_pushButton_18_clicked();

    void on_pushButton_13_clicked();

    void on_pushButton_19_clicked();



    void on_pushButton_query_clicked();



    void on_pushButton_referto_clicked();

    void on_pushButton_cancel_clicked();

    void on_pushButton_warnupload_clicked();

    void on_pushButton_save6_clicked();

    void on_pushButton_exit6_clicked();

    void on_pushButton_warnexit_clicked();

    void on_horizontalSlider_sliderMoved(int position);


    void on_horizontalSlider_2_sliderMoved(int position);

    void on_horizontalSlider_audio1_sliderMoved(int position);

    void on_pushButton_audio5_clicked();

    void on_pushButton_audio7_clicked();

    void on_pushButton_21_clicked();

    void on_pushButton_22_clicked();

    void on_pushButton_skip_clicked();
   // void on_label_WOKIcon_Clicked();
    void showCancelImportDialog(int SerialportNum) ;
    void on_pushButton_2111_clicked();
    void on_lineEdit_Clicked();
    void on_lineEdit_UserID_Clicked();
    void on_label_Clicked();
    void on_textBrowser_Clicked();
    void onItemClicked(const QModelIndex &index);
    void on_pushButton_111111_clicked();

    void on_pushButton_switch_clicked();

    void on_pushButton_fastforward_clicked();

    void on_pushButton_PFrame_clicked();

    void on_horizontalSlider_valueChanged(int value);

    void on_pushButton_PreviousPhoto_clicked();

    void on_pushButton_NextPhoto_clicked();

    void on_pushButton_OPEN_clicked();

    void on_pushButton_OpenAudio_clicked();

protected:
    // void paintEvent(QPaintEvent *e);
protected:
    bool eventFilter(QObject *watched, QEvent *event);       /* 事件过滤 */
private:
    Ui::MainWindow *ui;
    QMediaPlayer *pPlaye;
    QMediaPlayer *pPlaye1;
    QMediaPlaylist *pPlaylist;
    QMediaPlaylist *pPlaylist1;
    //QVideoWidget *pViedo;
    QMediaPlayer *pPlayeAudio;
    QMediaPlaylist *pPlayAudiolist;
    //QVideoWidget *pAudio;
QMediaPlayer *MediaPlayer;
    Qt::WindowState  m_eCurrentWindowState;     /* 当前窗口状态 */
    Qt::WindowState  m_eOldWindowState;         /* 之前窗口状态 */
    QTimer* timer;
    QTableWidgetItem *item[10000];
    QTableWidgetItem *item1[100];
    QTableWidgetItem *item2[100];
    QTableWidgetItem *item3[100];
    QTableWidgetItem *item4[100];
    QSet<QString> currentDrives;


    QSystemTrayIcon trayIcon;
    ThreadInfo threadinfo;


    QStringList Noticelist;
     QStringListModel *model;
    //BindingInfo bindinginfo;
    bool isOpen;//是否打开视频播发器
    bool isOpen1;//是否打开音频播发器
    bool IsHide;//是否隐藏
    bool IsLock;//是否锁定
    int lastNum;//视频列表数量
    bool IsNotic=false;
    QFrame *frame;
    //bool isUpdate=true;

    //    QString totalTime;//总时间
    int currentTimeString;//目前播放时间
    QProcess *process;//使用进程进行操作，外部定义，mplayer处理
    QPixmap pixmap;//获取控件各种图片样式
    QString arg;//用来存放要使用的mplayer播放器路径地址
    int mplayervideotime;//定义一个int类型的变量获取mplayer视频播放进度的时间
    QString filename;//视频文件夹名称
};
#endif //MAINWINDOW_H
